{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "791f1c29-a576-4a9a-bf13-33637741b5ea",
   "metadata": {},
   "source": [
    "https://python.langchain.com/docs/use_cases/question_answering/\n",
    "\n",
    "https://milvus.io/docs/integrate_with_langchain.md"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "084290ce-570e-4b21-bf57-295c1784b586",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: langchain\n",
      "Version: 0.1.9\n",
      "Summary: Building applications with LLMs through composability\n",
      "Home-page: https://github.com/langchain-ai/langchain\n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: aiohttp, dataclasses-json, jsonpatch, langchain-community, langchain-core, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "!pip show langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fcf6c007-5aa4-4085-a230-fae1912fdfc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: pymilvus\n",
      "Version: 2.3.0\n",
      "Summary: Python Sdk for Milvus\n",
      "Home-page: \n",
      "Author: \n",
      "Author-email: Milvus Team <milvus-team@zilliz.com>\n",
      "License: \n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: environs, grpcio, pandas, protobuf, ujson\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "!pip show pymilvus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f391b535-1691-4509-ad2c-51d8024893f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Prepare the documents you want the LLM to peak at when it thinks.\n",
    "#Set up an embedding model to convert documents into vector embeddings.\n",
    "#Set up a vector store used to save the vector embeddings.\n",
    "from langchain.document_loaders import WebBaseLoader\n",
    "\n",
    "loader = WebBaseLoader([\n",
    "    # \"https://gitee.com/beyond-prototype/mistral/blob/master/README.md\",\n",
    "    \"https://milvus.io/docs/overview.md\",\n",
    "])\n",
    "\n",
    "docs = loader.load()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "26f05b88-5980-4975-8d00-c31199ed738c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8948\n"
     ]
    }
   ],
   "source": [
    "print(len(docs[0].page_content))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c52d1e6b-9a8a-42a4-bd10-67e18cead9d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
    "# text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)\n",
    "docs = text_splitter.split_documents(docs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "87969ae0-bfe3-4d56-9685-bd20b3b72e6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "print(len(docs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "691d13ce-ead7-4094-8236-4145d123f417",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings.ollama import OllamaEmbeddings\n",
    "\n",
    "ollama_url = \"http://ollama:11434\"\n",
    "\n",
    "embeddings = OllamaEmbeddings(base_url=ollama_url, model=\"mistral:instruct\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fd2e268c-65d5-4b64-b960-3154cc41b16d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set Milvus host and port\n",
    "MILVUS_HOST = \"milvus\"  # this is the service name defined in compose yaml\n",
    "MILVUS_PORT = \"19530\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "14b2c0ab-e21d-46da-9265-2289d442fdb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#from pymilvus import connections\n",
    "#connections.connect(\"default\", host=MILVUS_HOST, port=MILVUS_PORT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "85065dec-75cb-42b9-943d-d325ce413734",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain.vectorstores import Milvus\n",
    "\n",
    "vector_store = Milvus.from_documents(\n",
    "    docs,\n",
    "    embedding=embeddings,\n",
    "    connection_args={\"host\": MILVUS_HOST, \"port\": MILVUS_PORT}\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "715c38e6-7656-411a-9289-827b51c8dd91",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"What is Milvus?\"\n",
    "docs = vector_store.similarity_search(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7eb0d96f-5663-4541-9bdb-db6fe1348844",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(page_content='Vector similarity search\\nVector similarity search is the process of comparing a vector to a database to find vectors that are most similar to the query vector. Approximate nearest neighbor (ANN) search algorithms are used to accelerate the searching process. If the two embedding vectors are very similar, it means that the original data sources are similar as well.\\nWhy Milvus?', metadata={'source': 'https://milvus.io/docs/overview.md', 'title': 'Introduction Milvus documentation', 'description': 'Milvus is an open-source vector database designed specifically for AI application development, embeddings similarity search, and MLOps v2.3.x.', 'language': 'en', 'pk': 447208569711671871}), Document(page_content='Vector similarity search\\nVector similarity search is the process of comparing a vector to a database to find vectors that are most similar to the query vector. Approximate nearest neighbor (ANN) search algorithms are used to accelerate the searching process. If the two embedding vectors are very similar, it means that the original data sources are similar as well.\\nWhy Milvus?', metadata={'source': 'https://milvus.io/docs/overview.md', 'title': 'Introduction Milvus documentation', 'description': 'Milvus is an open-source vector database designed specifically for AI application development, embeddings similarity search, and MLOps v2.3.x.', 'language': 'en', 'pk': 447783463267604839}), Document(page_content='Vector similarity search\\nVector similarity search is the process of comparing a vector to a database to find vectors that are most similar to the query vector. Approximate nearest neighbor (ANN) search algorithms are used to accelerate the searching process. If the two embedding vectors are very similar, it means that the original data sources are similar as well.\\nWhy Milvus?', metadata={'source': 'https://milvus.io/docs/overview.md', 'title': 'Introduction Milvus documentation', 'description': 'Milvus is an open-source vector database designed specifically for AI application development, embeddings similarity search, and MLOps v2.3.x.', 'language': 'en', 'pk': 447177552328659028}), Document(page_content='Vector similarity search\\nVector similarity search is the process of comparing a vector to a database to find vectors that are most similar to the query vector. Approximate nearest neighbor (ANN) search algorithms are used to accelerate the searching process. If the two embedding vectors are very similar, it means that the original data sources are similar as well.\\nWhy Milvus?', metadata={'source': 'https://milvus.io/docs/overview.md', 'title': 'Introduction Milvus documentation', 'description': 'Milvus is an open-source vector database designed specifically for AI application development, embeddings similarity search, and MLOps v2.3.x.', 'language': 'en', 'pk': 447965379944258376})]\n"
     ]
    }
   ],
   "source": [
    "print(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7b8993a0-57f1-4d07-98e8-0bce4526ebc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# https://milvus.io/docs/integrate_with_langchain.md#Ask-your-question\n",
    "\n",
    "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n",
    "\n",
    "from langchain.llms import Ollama\n",
    "\n",
    "# The following code snippet sets up a chain using mistral:instruct as the LLM and map-reduce the chain type.\n",
    "# The returned results include both the intermediate_steps and output_text. \n",
    "# The former indicates what documents it refers to during the search, and the latter is the final answer to the question.\n",
    "\n",
    "chain = load_qa_with_sources_chain(Ollama(base_url=ollama_url,model=\"mistral:instruct\"), chain_type=\"map_reduce\", return_intermediate_steps=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cc535d24-6ac5-4251-b3be-02ff64aa0489",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from transformers import GPT2Tokenizer, GPT2LMHeadModel\n",
    "# tokernizer = GPT2Tokenizer.from_pretrained(\"/home/jovyan/work/models/gpt2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a30179d1-7e23-48bb-97e9-93b1e3ad1eec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from transformers import GPT2Tokenizer, GPT2Model\n",
    "# tokenizer = GPT2Tokenizer.from_pretrained(\"/home/jovyan/work/gpt2\")\n",
    "# model = GPT2Model.from_pretrained(\"/home/jovyan/work/gpt2\")\n",
    "# # model.save_pretrained(\"/home/jovyan/work/gpt2\")\n",
    "# text = \"Hello, how are you today?\"\n",
    "# input_ids = tokenizer.encode(text, return_tensors=\"pt\")\n",
    "# output = model(input_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ecde4d5e-1e8f-4a8a-afe0-6eb3b61a3e3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'intermediate_steps': [' \"Milvus is a vector database and search engine, which provides efficient and scalable solutions for vector similarity search using Approximate Nearest Neighbor (ANN) algorithms.\" - This text from the document may be relevant to answering the question as it explains that Milvus is a tool specifically designed for vector similarity search. However, the initial part of the text discussing the process of vector similarity search itself is not directly answering the question and should not be returned verbatim.',\n",
       "  ' \"Milvus is a vector database and similarity search engine. It provides efficient and scalable solutions for vector similarity search and approximate nearest neighbor (ANN) search.\"\\n\\n\"Why Milvus? Milvus is designed to handle large-scale vector databases and delivers high performance and low latency. It supports various similarity metrics and index types, making it flexible for different use cases. Milvus also provides an easy-to-use API and is open source.\"\\n\\nThe text above, although not directly quoting the portion about \"Vector similarity search,\" does answer the question by explaining what Milvus is and its relevance to vector similarity search.',\n",
       "  ' Milvus is not explicitly mentioned in the provided text. However, the text does discuss the concept of \"approximate nearest neighbor (ANN) search algorithms\" which are used to accelerate the process of vector similarity search. Milvus is an open-source vector database and search engine that provides efficient and scalable solutions for ANN searches on large vectors datasets. So, while Milvus is not directly mentioned in the text, it relates to the topic of vector similarity search using approximate nearest neighbor algorithms.',\n",
       "  ' \"Milvus is a vector database and machine learning framework. It provides efficient solutions for vector similarity search using Approximate Nearest Neighbor (ANN) search algorithms. Milvus can be used to compare vectors and find the most similar ones in large datasets.\"\\n\\nThe text above, from the given document, answers the question \"What is Milvus?\" by explaining that it is a vector database and machine learning framework that specializes in vector similarity search using Approximate Nearest Neighbor (ANN) algorithms.'],\n",
       " 'output_text': ' Milvus is an open-source vector database and machine learning framework that specializes in vector similarity search using Approximate Nearest Neighbor (ANN) algorithms. It provides efficient solutions for handling large datasets and supports various similarity metrics and index types, making it flexible for different use cases. Milvus is designed to handle large-scale vector databases and delivers high performance and low latency.\\n\\nSources: \\n\\n* <https://milvus.io/docs/overview.md>\\n* Text in the document discussing Approximate Nearest Neighbor (ANN) search algorithms.'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "query = \"What is Milvus?\"\n",
    "#query = \"How are you?\"\n",
    "\n",
    "chain.invoke({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
